/*********************************************
  2012-8-26
  此处需要一些相应表的生成
  **********************************************/
#ifndef DATABASE_H
#define DATABASE_H

#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include "tools/aqp/aqp.hpp"
#include "model/message.h"
#include <QVariant>


/**
 * @brief The DBcontrol class 封装了对数据库的操作
 */
class DBcontrol
{
public:
    /**
     * @brief createConnection
     * @return
     */
    static bool createConnection(const QString &dbFileName) {
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName(dbFileName);
        if (!db.open()) {
            AQP::warning(0,qApp->tr("不能打开数据库"),qApp->tr("不能建立到数据库的连接！"));
            return false;
        }

        QSqlQuery query;


        QString sql = "create table devs(name int primary key,des varchar(30) )";
        query.exec(sql);

        sql = "create table realTimeData(name int,_date date,_time time ,des varchar(10),value int)";
        query.exec(sql);

        sql = "create table badValue(name int,_date date,_time time,ex varchar(10),value int,des varchar(30))";
        query.exec(sql);

        sql = "create table hasSendSMS(phone varchar(13),_date date,_time time,con varchar(140))";
        query.exec(sql);

        sql = "create table notSendSMS(phone varchar(13),_date date,_time time,con varchar(140))";
        query.exec(sql);

        sql = "create table control(phone varchar(13),_date date,_time time,flag int)";
        query.exec(sql);



        sql = "insert into devs values(66,'I am a Coordinator and I am in XX1')";
        query.exec(sql);

        sql = "insert into devs values(67,'I am a Enddevice and I am in XX2')";
        query.exec(sql);

        sql = "insert into devs values(68,'I am a Enddevice and I am in XX2')";
        query.exec(sql);

        sql = "insert into devs values(69,'I am a Enddevice and I am The XX3')";
        query.exec(sql);

        return true;
    }
    /**
     * @brief selectDevDes 根据设备名得到数据库中的设备描述
     * @param name
     * @return
     */
    static QString selectDevDes(ushort name) {
        QSqlQuery query;
        QString sql = QObject::trUtf8("select des from devs where name = %1").arg(name);
        if (query.exec(sql) && query.next()) {
            return query.value(0).toString().toUtf8();
        }
        return QString();
    }

    /**
     * @brief insertAmes 将已发送的信息插入到表中
     * @param me
     * @return
     */
    static bool insertAmes(const Message &me) {
        QSqlQuery query;
        query.prepare("insert into  hasSendSMS values(?,?,?,?)");
        query.addBindValue(me.getPhone());
        query.addBindValue(me.getDate());
        query.addBindValue(me.getTime());
        query.addBindValue(me.getContent());
        return query.exec();
    }

    /**
     * @brief insertAuMes 将发送失败的信息插入到表中
     * @param me
     * @return
     */
    static bool insertAuMes(const Message &me) {
        QSqlQuery query;
        query.prepare("insert into  notSendSMS values(?,?,?,?)");
        query.addBindValue(me.getPhone());
        query.addBindValue(me.getDate());
        query.addBindValue(me.getTime());
        query.addBindValue(me.getContent());
        return query.exec();
    }

    /**
     * @brief insertArealData 将实时数据插入表中
     * @return
     */
    static bool insertArealData(const QStringList &arg) {

        QSqlQuery query;
        query.prepare("insert into  realTimeData values(?,?,?,?,?)");
        query.addBindValue(arg.at(0));
        query.addBindValue(arg.at(1));
        query.addBindValue(arg.at(2));
        query.addBindValue(arg.at(3));
        query.addBindValue(arg.at(4));
        return query.exec();
    }

    /**
     * @brief insertBadValue 插入坏值
     * @return
     */
    static bool insertBadValue(const QStringList &arg) {
        QSqlQuery query;
        query.prepare("insert into  badValue values(?,?,?,?,?,?)");
        query.addBindValue(arg.at(0));
        query.addBindValue(arg.at(1));
        query.addBindValue(arg.at(2));
        query.addBindValue(arg.at(3));
        query.addBindValue(arg.at(4));
        query.addBindValue(arg.at(5));
        return query.exec();
    }

private:
    DBcontrol() {}
};

#endif


